﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.AdvTree;
using PPI.Client.Logic;
using PPI.Client.Logic.Inspection;
using PPI.Client.Logic.XML;
using PPI.Entities;
using PPI.Entities.Enumerator;
using PPI.Entities.Inspections;
using ColumnHeader = DevComponents.AdvTree.ColumnHeader;

namespace PPI.Controls.Grid
{
    public partial class TreeGridGPS : TreeGridBase
    {
        public TreeGridGPS()
        {
            InitializeComponent();
            this.cmiAction.Enabled = true;
        }

        protected List<EVendor_Visit_XMissionStructure> m_Structures;
        public List<EVendor_Visit_XMissionStructure> Structures
        {
            get { return m_Structures; }
            set { m_Structures = value; }
        }

        protected override bool ShowMenu()
        {
            return GetChildId(treeGrid.SelectedNode) != Int32.MinValue;
        }
        protected override void LoadTree()
        {
            treeGrid.Nodes.Clear();
            treeGrid.Columns.Clear();
            m_ChildElementStyle.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(234)))), ((int)(((byte)(239)))), ((int)(((byte)(242)))));


            #region Columns

            #region BasePositionCollumns

            ColumnHeader chLatitude = new ColumnHeader("Latitude");
            chLatitude.Width.Absolute = 100;

            ColumnHeader chLongitude = new ColumnHeader("Longitude");
            chLongitude.Width.Absolute = 100;

            ColumnHeader chAltitude = new ColumnHeader("Altitude");
            chAltitude.Width.Absolute = 100;

            ColumnHeader chSatelliteCount = new ColumnHeader("Satellites");
            chSatelliteCount.Width.Absolute = 100;

            ColumnHeader chSatelliteTime = new ColumnHeader("Time");
            chSatelliteTime.Width.Absolute = 100;

            ColumnHeader chDate = new ColumnHeader("Date");
            chDate.Width.Absolute = 100;



            #endregion

            ColumnHeader chVisit = new ColumnHeader("Structure");
            chVisit.Width.Absolute = 100;

            ColumnHeader chProject = new ColumnHeader("Project");
            chProject.Width.Absolute = 100;

            ColumnHeader chContact = new ColumnHeader("Contact");
            chContact.Width.Absolute = 100;
            
            ColumnHeader chVisitType = new ColumnHeader("Visit Type");
            chVisitType.Width.Absolute = 100;

            ColumnHeader chTopType = new ColumnHeader("Top Type");
            chTopType.Width.Absolute = 100;

            ColumnHeader chAsset = new ColumnHeader("Asset");
            chAsset.Width.Absolute = 100;

            #endregion

            treeGrid.Columns.Add(chVisit);
            treeGrid.Columns.Add(chAsset);

            treeGrid.Columns.Add(chLatitude);
            treeGrid.Columns.Add(chLongitude);
            treeGrid.Columns.Add(chAltitude);
            treeGrid.Columns.Add(chSatelliteCount);
            treeGrid.Columns.Add(chSatelliteTime);
            treeGrid.Columns.Add(chDate);

            treeGrid.Columns.Add(chProject);
            treeGrid.Columns.Add(chContact);
            treeGrid.Columns.Add(chVisitType);
            treeGrid.Columns.Add(chTopType);

            foreach (EVendor_Visit_XMissionStructure visit in m_Structures)
            {
                Node visitNode = CreateStructureNode(visit);
                treeGrid.Nodes.Add(visitNode);
            }
            
        }

        private Node CreateStructureNode(EVendor_Visit_XMissionStructure structure)
        {
            Node visitNode = new Node();

            visitNode.Text = structure.Id.ToString();
            visitNode.DataKey = structure;
            if (structure.UtilityAsset.Id != Int32.MinValue)
                visitNode.Cells.Add(new Cell(structure.UtilityAsset.Id.ToString()));
            else
                visitNode.Cells.Add(new Cell(""));

            visitNode.Cells.Add(new Cell(structure.Latitude.ToString()));
            visitNode.Cells.Add(new Cell(structure.Longitude.ToString()));
            visitNode.Cells.Add(new Cell(structure.Altitude.ToString()));
            visitNode.Cells.Add(new Cell(structure.SatelliteCount.ToString()));
            visitNode.Cells.Add(new Cell(structure.SatelliteTime.ToLongDateString()));
            if (structure.Date != DateTime.MinValue)
                visitNode.Cells.Add(new Cell(structure.Date.ToLongDateString()));
            else
                visitNode.Cells.Add(new Cell(""));
            visitNode.Cells.Add(new Cell(structure.Vendor_Project.Name));
            visitNode.Cells.Add(new Cell(structure.Vendor_Contact.Name));
            visitNode.Cells.Add(new Cell(structure.Vendor_Visit_Type.Name));
            visitNode.Cells.Add(new Cell(structure.TopType.Name));


            #region ChildCollumns

            #region BasePositionCollumns

            ColumnHeader chLatitude = new ColumnHeader("Latitude");
            chLatitude.Width.Absolute = 100;

            ColumnHeader chLongitude = new ColumnHeader("Longitude");
            chLongitude.Width.Absolute = 100;

            ColumnHeader chAltitude = new ColumnHeader("Altitude");
            chAltitude.Width.Absolute = 100;

            ColumnHeader chSatelliteCount = new ColumnHeader("Satellites");
            chSatelliteCount.Width.Absolute = 100;

            ColumnHeader chSatelliteTime = new ColumnHeader("Time");
            chSatelliteTime.Width.Absolute = 100;

            ColumnHeader chDate = new ColumnHeader("Date");
            chDate.Width.Absolute = 100;



            #endregion

            ColumnHeader chPoleId = new ColumnHeader("ID");
            chPoleId.Width.Absolute = 100;

            ColumnHeader chYear = new ColumnHeader("Year");
            chYear.Width.Absolute = 100;

            ColumnHeader chAID = new ColumnHeader("AID");
            chAID.Width.Absolute = 100;


            ColumnHeader chLastVisit = new ColumnHeader("Last Visit");
            chLastVisit.Width.Absolute = 100;

            ColumnHeader chLVisitor = new ColumnHeader("Last Visitor");
            chLastVisit.Width.Absolute = 100;

            ColumnHeader chNumber = new ColumnHeader("Number");
            chNumber.Width.Absolute = 100;

            ColumnHeader chTreatment = new ColumnHeader("Treatment");
            chTreatment.Width.Absolute = 100;

            ColumnHeader chClass = new ColumnHeader("Class");
            chClass.Width.Absolute = 100;

            ColumnHeader chSpecie = new ColumnHeader("Specie");
            chSpecie.Width.Absolute = 100;

            ColumnHeader chTopType = new ColumnHeader("Top Type");
            chTopType.Width.Absolute = 100;

            ColumnHeader chLength = new ColumnHeader("Length");
            chLength.Width.Absolute = 100;


            visitNode.NodesColumns.Add(chPoleId);
            visitNode.NodesColumns.Add(chLatitude);
            visitNode.NodesColumns.Add(chLongitude);
            visitNode.NodesColumns.Add(chAltitude);
            visitNode.NodesColumns.Add(chSatelliteCount);
            visitNode.NodesColumns.Add(chSatelliteTime);
            visitNode.NodesColumns.Add(chDate);

            visitNode.NodesColumns.Add(chYear);
            visitNode.NodesColumns.Add(chAID);
            visitNode.NodesColumns.Add(chLastVisit);
            visitNode.NodesColumns.Add(chLVisitor);
            visitNode.NodesColumns.Add(chNumber);
            visitNode.NodesColumns.Add(chTreatment);
            visitNode.NodesColumns.Add(chClass);
            visitNode.NodesColumns.Add(chSpecie);
            visitNode.NodesColumns.Add(chTopType);
            visitNode.NodesColumns.Add(chLength);

            #endregion

            foreach (EVendor_Visit_XMissionStructures_Pole pole in structure.Poles)
            {
                Node poleNode = new Node();
                poleNode.DataKey = pole;
                poleNode.Style = m_ChildElementStyle;
                poleNode.Text = pole.Id.ToString();
                poleNode.Cells.Add(new Cell(pole.Latitude.ToString()));
                poleNode.Cells.Add(new Cell(pole.Longitude.ToString()));
                poleNode.Cells.Add(new Cell(pole.Altitude.ToString()));
                poleNode.Cells.Add(new Cell(pole.SatelliteCount.ToString()));
                poleNode.Cells.Add(new Cell(pole.SatelliteTime.ToLongDateString()));

                if (pole.Date != DateTime.MinValue)
                    poleNode.Cells.Add(new Cell(pole.Date.ToLongDateString()));
                else
                    poleNode.Cells.Add(new Cell(""));

                if (pole.Year > 0)
                    poleNode.Cells.Add(new Cell(pole.Year.ToString()));
                else
                    poleNode.Cells.Add(new Cell("-"));

                poleNode.Cells.Add(new Cell(pole.AID));

                if (pole.LVisit != Int32.MinValue)
                    poleNode.Cells.Add(new Cell(pole.LVisit.ToString()));
                else
                    poleNode.Cells.Add(new Cell("-"));

                poleNode.Cells.Add(new Cell(pole.Number));
                poleNode.Cells.Add(new Cell(pole.Treatment.Name));
                poleNode.Cells.Add(new Cell(pole.Class.Name));
                poleNode.Cells.Add(new Cell(pole.Species.Name));
                poleNode.Cells.Add(new Cell(pole.TopType.Name));
                poleNode.Cells.Add(new Cell(pole.Length.ToString()));
                visitNode.Nodes.Add(poleNode);
            }
            return visitNode;
        }

        public void LoadStructures(string connectionString)
        {
            DatabaseTypes mobileBaseType =LOutbox.GetDatabaseType(connectionString);
            if (mobileBaseType == DatabaseTypes.Project)
                m_Structures = LVendor_Visit_XMissionStructure.GetMobileProject(connectionString);
            else
                m_Structures = LVendor_Visit_XMissionStructure.GetMobileVisits(connectionString);
            LoadTree();
        }

        public void RefreshStructures(List<EVendor_Visit_XMissionStructure> structures)
        {
            m_Structures = structures;
            LoadTree();
        }
    }
}
